/*
 * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

#ifndef RAT_PHYSICS_BODY_MANAGER
#define RAT_PHYSICS_BODY_MANAGER

#include "common.h"
#include "arbiter.h"
#include "body.h"
#include "collision.h"

typedef enum rat_body_manager_type
{
	rat_body_manager_type_null=0,
	rat_body_manager_type_array_table=1,
	rat_body_manager_type_quad_tree=2
} rat_body_manager_type;

struct rat_body_manager;
struct rat_world;

typedef struct rat_body_manager_class
{
	void (*destroy)(struct rat_body_manager *table);
	void (*add_body)(struct rat_body_manager *table,rat_body *body);
	void (*remove_body)(struct rat_body_manager *table,rat_body *body);
	void (*remove_and_destroy_body)(struct rat_body_manager *table,rat_body *body);
	void (*clear_bodies)(struct rat_body_manager *table);
	void (*free_bodies)(struct rat_body_manager *table);
	void (*build_spatial_index)(struct rat_body_manager *table);
	void (*collide_bodies)(struct rat_body_manager *table);
	unsigned int (*count_all_bodies)(struct rat_body_manager *table);
	unsigned int (*count_static_bodies)(struct rat_body_manager *table);
	unsigned int (*count_dynamic_bodies)(struct rat_body_manager *table);
	unsigned int (*count_arbiters)(struct rat_body_manager *table);
	rat_iterator *(*all_bodies_iterator)(struct rat_body_manager *table);
	rat_iterator *(*static_bodies_iterator)(struct rat_body_manager *table);
	rat_iterator *(*dynamic_bodies_iterator)(struct rat_body_manager *table);
	rat_iterator *(*arbiters_iterator)(struct rat_body_manager *table);
} rat_body_manager_class;

typedef struct rat_body_manager
{
	rat_body_manager_type type;
	rat_body_manager_class *clas;

	struct rat_world *world_ref;
} rat_body_manager;

typedef struct rat_body_array_iterator
{
	rat_iterator_class *clas;
	rat_block_allocator *bal;

	unsigned int numbods,i;
	rat_body **bodies;
} rat_body_array_iterator;

typedef struct rat_arbiter_array_iterator
{
	rat_iterator_class *clas;
	rat_block_allocator *bal;

	unsigned int numarbs,i;
	rat_arbiter **arbiters;
} rat_arbiter_array_iterator;

#ifdef __cplusplus
extern "C" {
#endif

// body managers are for internal use only.  not that
// you can't, it's just that it would be kinda pointless

extern unsigned int collision_persistence;

void rat_body_manager_destroy(rat_body_manager *table);

void rat_body_manager_add_body(rat_body_manager *table,rat_body *body);
void rat_body_manager_remove_body(rat_body_manager *table,rat_body *body);
void rat_body_manager_remove_and_destroy_body(rat_body_manager *table,rat_body *body);
void rat_body_manager_clear_bodies(rat_body_manager *table);
void rat_body_manager_free_bodies(rat_body_manager *table);

void rat_body_manager_build_spatial_index(rat_body_manager *table);
void rat_body_manager_collide_bodies(rat_body_manager *table);

unsigned int rat_body_manager_count_all_bodies(rat_body_manager *table);
unsigned int rat_body_manager_count_static_bodies(rat_body_manager *table);
unsigned int rat_body_manager_count_dynamic_bodies(rat_body_manager *table);
unsigned int rat_body_manager_count_arbiters(rat_body_manager *table);

rat_iterator *rat_body_manager_all_bodies_iterator(rat_body_manager *table);
rat_iterator *rat_body_manager_static_bodies_iterator(rat_body_manager *table);
rat_iterator *rat_body_manager_dynamic_bodies_iterator(rat_body_manager *table);
rat_iterator *rat_body_manager_arbiters_iterator(rat_body_manager *table);

void rat_body_array_next(rat_iterator *iter);
int rat_body_array_finished(rat_iterator *iter);
void *rat_body_array_value(rat_iterator *iter);
void rat_body_array_destroy(rat_iterator *iter);

void rat_arbiter_array_next(rat_iterator *iter);
int rat_arbiter_array_finished(rat_iterator *iter);
void *rat_arbiter_array_value(rat_iterator *iter);
void rat_arbiter_array_destroy(rat_iterator *iter);

extern rat_body_manager_class rat_array_table_class;
extern rat_body_manager_class rat_quad_tree_class;

void rat_init_body_manager_classes();

#ifdef __cplusplus
};
#endif

#endif
